import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common.dart';

class MallAddress extends StatefulWidget {
  @override
  State<MallAddress> createState() => _SettingPageState();
}

class _SettingPageState extends State<MallAddress> {
  List addressList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getdatd();
  }

  getdatd() {
    getData(url: '/addresses').then((res) {
      addressList = res['addresses'];
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xffffffff),
        title: Text('收货地址'),
        centerTitle: true,
        leading: buildBackButton(context),
      ),
      body: Column(
        children: [
          if (addressList.isEmpty) isdata(),
          Expanded(
            child: ListView(
              padding: EdgeInsets.symmetric(horizontal: 15),
              children: [
                for (var item in addressList.asMap().entries)
                  GestureDetector(
                    onTap: () {
                      putData(
                        url: '/addresses/${item.value['uuid']}',
                        data: {'is_default': '1'},
                      ).then((res) {
                        back();
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.only(bottom: 15),
                      padding: EdgeInsets.all(15),
                      decoration: BoxDecoration(
                        color: Color(0XFFF8F9FD),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '${item.value['consignee']}    ${item.value['phone']}',
                            style: TextStyle(
                              fontSize: 14,
                              color: Color(0XFF0F172B),
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(height: 5),
                          Text(
                            '${item.value['province']} ${item.value['city']} ${item.value['district']} ${item.value['address']}',
                            style: TextStyle(
                              fontSize: 14,
                              color: Color(0XFF999999),
                            ),
                          ),

                          SizedBox(height: 15),

                          Row(
                            children: [
                              if (item.value['is_default'] == 1)
                                Text(
                                  '默认地址',
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Color(0XFF1F0584),
                                  ),
                                ),
                              Expanded(child: SizedBox()),
                              GestureDetector(
                                onTap: () {
                                  Get.dialog(
                                    AlertDialog(
                                      backgroundColor: Colors.transparent,
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(5),
                                      ),
                                      content: Stack(
                                        children: [
                                          Image.asset(
                                            'static/tan.png',
                                            width: 255,
                                            fit: BoxFit.fitWidth,
                                          ),
                                          Positioned(
                                            top: 80,
                                            right: 0,
                                            left: 0,
                                            child: Column(
                                              children: [
                                                Text(
                                                  '提示',
                                                  style: TextStyle(
                                                    fontSize: 24,
                                                    color: Color(0XFF0F172B),
                                                  ),
                                                ),
                                                SizedBox(height: 10),
                                                Text(
                                                  '确定要删除吗',
                                                  style: TextStyle(
                                                    fontSize: 14,
                                                    color: Color(0XFF0F172B),
                                                  ),
                                                ),
                                                SizedBox(height: 30),
                                                GestureDetector(
                                                  onTap: () {
                                                    delData(
                                                      url:
                                                          '/addresses/${item.value['uuid']}',
                                                    ).then((res) {
                                                      if (res != null) {
                                                        Get.back();
                                                        addressList.removeAt(
                                                          item.key,
                                                        );
                                                        setState(() {});
                                                        showTost('删除成功');
                                                      }
                                                    });
                                                  },
                                                  child: Container(
                                                    width: 210,
                                                    height: 45,
                                                    decoration: BoxDecoration(
                                                      color: Color(0XFF0F172B),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                            45,
                                                          ),
                                                    ),
                                                    alignment: Alignment.center,
                                                    child: Text(
                                                      '确认',
                                                      style: TextStyle(
                                                        fontSize: 18,
                                                        color: Color(
                                                          0XFFFFFFFF,
                                                        ),
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                                SizedBox(height: 10),
                                                GestureDetector(
                                                  onTap: () {
                                                    Get.back();
                                                  },
                                                  child: Text(
                                                    '取消',
                                                    style: TextStyle(
                                                      fontSize: 18,
                                                      color: Color(0XFF0F172B),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  );
                                },
                                child: Row(
                                  children: [
                                    Image.asset(
                                      'static/del.png',
                                      width: 15,
                                      fit: BoxFit.fitWidth,
                                    ),
                                    Text(
                                      '删除',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF999999),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              SizedBox(width: 30),
                              GestureDetector(
                                onTap: () {
                                  toNamed(
                                    '/web_tabs/shopping/mall_up_addrerss?edit=1&address=${jsonEncode(item.value)}',
                                  ).then((res) {
                                    getdatd();
                                  });
                                },
                                child: Row(
                                  children: [
                                    Image.asset(
                                      'static/edit.png',
                                      width: 12,
                                      fit: BoxFit.fitWidth,
                                    ),
                                    Text(
                                      '编辑',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF999999),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
              ],
            ),
          ),
          GestureDetector(
            onTap: () {
              toNamed('/web_tabs/shopping/mall_up_addrerss').then((res) {
                getdatd();
              });
            },
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
              height: 50,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0XFF0F172B),
                borderRadius: BorderRadius.circular(50),
              ),
              child: Text(
                '新增收货地址',
                style: TextStyle(fontSize: 18, color: Color(0XFFFFFFFF)),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
